Nesse arquivo foi usada a seguinte estratégia:
Utilizei um recurso chamado DTW (Dynamic Time Warping) onde se calcula a dissimilaridades entre as séries temporais. Nesse caso entre cada movimento ou path realizado pelo usuário. Dessa forma podemos contabilizar o quão similar as series são entre si.
Com essa estratégia podemos calcular uma distancia
e assim realizar procedimentos como clusterizações e até classificações. Nesse caso utilizei um simples KNN(k-nearest neighbors) para realizar a classificação binária.
Importante dizer que esse método (DTW) é robusto para séries com múltiplas variaveis (multivariate).
Obs. Podemos melhorar essa abordagem adicionando um filtro Kernel na contrução de um modelo KNN mais robusto.
In [20]:
library(dtw)
library(KODAMA)
library(caret)
library(pROC)
groups <- read.csv(file="./MovementAAL/groups/MovementAAL_DatasetGroup.csv",head=TRUE,sep=",")
targetAll <- read.csv(file="./MovementAAL/dataset/MovementAAL_target.csv",head=TRUE,sep=",")
In [21]:
#Group 1
allDataGroup1<-list()
allDataGroup1Target<-list()
groups1 = groups[groups$dataset_ID==1, ]
index<-1
for (id in groups1$X.sequence_ID){
caminho <-paste("./MovementAAL/dataset/MovementAAL_RSS_",id,".csv",sep="")
allDataGroup1[[index]]<-read.csv(file=caminho,head=TRUE,sep=",")
allDataGroup1Target[index]<-targetAll[[2]][id]
index<-index+1
}
kdist <- dist(allDataGroup1, method="DTW",diag = TRUE,upper = TRUE)
kdistM<- as.matrix(kdist)
In [22]:
target <- unlist(allDataGroup1Target)
allDataGroup1TargetFrame = as.data.frame(target)
train <- createDataPartition(allDataGroup1TargetFrame$target, p = .7, list = TRUE,times=10)
y <- allDataGroup1TargetFrame$target
In [23]:
allAccuracyGroup1 <- c()
for( i in 1:length(train)){
test <- (1:nrow(allDataGroup1TargetFrame))[-train[[i]]]
preds <- knn.predict(train[[i]], test,y, kdistM, k=3,agg.meth="majority")
matrix <- confusionMatrix(preds, y[test])
#accuracy
allAccuracyGroup1 <- c(allAccuracyGroup1,matrix[3]$overall[[1]])
}
mean(allAccuracyGroup1)
sd(allAccuracyGroup1)
Out[23]:
Out[23]:
In [24]:
#Group 2
allDataGroup2<-list()
groups2 = groups[groups$dataset_ID==2, ]
allDataGroup2Target<-list()
index<-1
for (id in groups2$X.sequence_ID){
caminho <-paste("./MovementAAL/dataset/MovementAAL_RSS_",id,".csv",sep="")
allDataGroup2[[index]]<-read.csv(file=caminho,head=TRUE,sep=",")
allDataGroup2Target[index]<-targetAll[[2]][id]
index<-index+1
}
kdist <- dist(allDataGroup2, method="DTW",diag = TRUE,upper = TRUE)
kdistM<- as.matrix(kdist)
In [25]:
target <- unlist(allDataGroup2Target)
allDataGroup2TargetFrame = as.data.frame(target)
train <- createDataPartition(allDataGroup2TargetFrame$target, p = .7, list = TRUE,times=10)
y <- allDataGroup2TargetFrame$target
In [26]:
allAccuracyGroup2 <- c()
for( i in 1:length(train)){
test <- (1:nrow(allDataGroup2TargetFrame))[-train[[i]]]
preds <- knn.predict(train[[i]], test,y, kdistM, k=3,agg.meth="majority")
matrix <- confusionMatrix(preds, y[test])
#accuracy
allAccuracyGroup2 <- c(allAccuracyGroup2,matrix[3]$overall[[1]])
}
mean(allAccuracyGroup2)
sd(allAccuracyGroup2)
Out[26]:
Out[26]:
In [27]:
#Group 3
allDataGroup3<-list()
groups3 = groups[groups$dataset_ID==3, ]
allDataGroup3Target<-list()
index<-1
for (id in groups3$X.sequence_ID){
caminho <-paste("./MovementAAL/dataset/MovementAAL_RSS_",id,".csv",sep="")
allDataGroup3[[index]]<-read.csv(file=caminho,head=TRUE,sep=",")
allDataGroup3Target[index]<-targetAll[[2]][id]
index<-index+1
}
kdist <- dist(allDataGroup3, method="DTW",diag = TRUE,upper = TRUE)
kdistM<- as.matrix(kdist)
In [28]:
target <- unlist(allDataGroup3Target)
allDataGroup3TargetFrame = as.data.frame(target)
train <- createDataPartition(allDataGroup3TargetFrame$target, p = .7, list = TRUE,times=10)
y <- allDataGroup3TargetFrame$target
In [29]:
allAccuracyGroup3 <- c()
for( i in 1:length(train)){
test <- (1:nrow(allDataGroup3TargetFrame))[-train[[i]]]
preds <- knn.predict(train[[i]], test,y, kdistM, k=3,agg.meth="majority")
matrix <- confusionMatrix(preds, y[test])
#accuracy
allAccuracyGroup3 <- c(allAccuracyGroup3,matrix[3]$overall[[1]])
}
mean(allAccuracyGroup3)
sd(allAccuracyGroup3)
Out[29]:
Out[29]:
In [30]:
#All DataBases
targetAll <- read.csv(file="./MovementAAL/dataset/MovementAAL_target.csv",head=TRUE,sep=",")
allData<-list()
allDataTarget<-list()
index<-1
for (id in targetAll$X.sequence_ID){
caminho <-paste("./MovementAAL/dataset/MovementAAL_RSS_",id,".csv",sep="")
allData[[index]]<-read.csv(file=caminho,head=TRUE,sep=",")
allDataTarget[index]<-targetAll[[2]][id]
index<-index+1
}
kdist <- dist(allData, method="DTW",diag = TRUE,upper = TRUE)
kdistM<- as.matrix(kdist)
In [31]:
target <- unlist(allDataTarget)
allDataTargetFrame = as.data.frame(target)
train <- createDataPartition(allDataTargetFrame$target, p = .7, list = TRUE,times=10)
y <- allDataTargetFrame$target
In [32]:
allAccuracy <- c()
for( i in 1:length(train)){
test <- (1:nrow(allDataTargetFrame))[-train[[i]]]
preds <- knn.predict(train[[i]], test,y, kdistM, k=3,agg.meth="majority")
matrix <- confusionMatrix(preds, y[test])
#accuracy
allAccuracy <- c(allAccuracyGroup3,matrix[3]$overall[[1]])
}
mean(allAccuracy)
sd(allAccuracy)
Out[32]:
Out[32]:
In [35]:
#All groups datasets Confusion Matrix
target <- unlist(allDataTarget)
allDataTargetFrame = as.data.frame(target)
train <- createDataPartition(allDataTargetFrame$target, p = .7, list = TRUE,times=1)
y <- allDataTargetFrame$target
y <- factor(unlist(y),labels=c("No", "Yes"))
test <- (1:nrow(allDataTargetFrame))[-train[[1]]]
preds <- knn.predict(train[[1]], test,y, kdistM, k=3,agg.meth="majority")
classes <- factor(unlist( y[test]),labels=c("No", "Yes"))
matrix <- confusionMatrix(preds,classes)
matrix
Out[35]:
In [36]:
#ROC CURVE AND AUC
predsProb <- t(knn.probability(train[[1]], test,y, kdistM, k=3))
outcome<- predsProb[,1]
rocobj <- roc(classes, outcome,levels=c("No", "Yes"))
plot(rocobj)
Out[36]:
In [ ]: